﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListWidgetItem>

#include "singleton.h"
#include "scripteditdialog.h"
#include "showlogdialog.h"
#include "addclientdialog.h"
#include "cwebsocketclient.h"
#include "aboutdialog.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

#define IDD_MESSAGE_HEART_BEAT          100                  // 心跳消息
#define IDD_MESSAGE_LOG                 101                  // 日志消息
#define IDD_MESSAGE_GETSCRIPT_FILES     102                  // 得到当前系统使用的所有脚本文件
#define IDD_MESSAGE_GETSCRIPT_CONTENT   103                  // 得到指定脚本文件的内容
#define IDD_MESSAGE_UPDATESCRIPT        104                  // 更新指定的脚本文件

struct tagClients
{
    tagClients(){}
    tagClients(QString pip,int pport,int pdebugport)
        : ip(pip),port(pport),debugport(pdebugport) {}

    QString ip;
    int port,debugport;
};

class MainWindow : public QMainWindow , public Singleton<MainWindow>
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QString JsonToString(const QJsonObject& json);
    QJsonObject StringToJson(const QString& str);
    inline CWebSocketClient* getCurrentSocketClient(void) { return m_currentWebSocketClient; }
    inline tagClients* getCurrentClient(void) { return m_currentclient; }

public slots:
    void onprocesssocketmessage(CWebSocketClient *pwebsocket,QString message);
    void onprocesssocketconnected(CWebSocketClient *pwebsocket);
    void onprocesssocketdisconnected(CWebSocketClient *pwebsocket);
    void onprocessscriptfileitemDoubleClicked(QString filepath);
    void onprocessscriptfileupdate(QString filepath,QString content);

private:
    /// 导入当前使用的机器
    void LoadClients(void);
    /// 根据ip和端口得到对应的机器
    tagClients* isExistClient(QString ip,int port,int debugport);
    /// 根据机器名称得到机器
    tagClients* getClientByName(QString clientname);

private slots:
    void on_action_triggered();

    void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
    void on_tabWidget_currentChanged(int index);
    void on_action_2_triggered();
    void on_listWidget_customContextMenuRequested(const QPoint &pos);
    void deleteSeedSlot();

private:
    Ui::MainWindow *ui;

    ScriptEditDialog *m_ScriptEditDialog;
    ShowLogDialog *m_ShowLogDialog;
    AddClientDialog *m_AddClientDialog;

    QVector<tagClients> m_Clients;
    CWebSocketClientManager m_CWebSocketClientManager;
    CWebSocketClient *m_currentWebSocketClient;
    tagClients *m_currentclient;
    AboutDialog *m_AboutDialog;
};
#endif // MAINWINDOW_H
